<template>
  <div class="search-wrapper">
    <div class="search-box">
      <div class="search-inner">
        <i class="search-icon"></i>
        <input v-model="query" type="text" v-focus placeholder="搜索活动/设计师/品牌" @blur="hanldeBlur" />
      </div>
    </div>
    <div class="tabs" v-if="isSearch">
      <div
        class="tab-item"
        v-for="(item, index) of tabs"
        :key="index"
        @click="tabItemClick(index)"
        :class="{'active': activeIndex === index}"
      >{{item}}</div>
      <div class="tab-angle" :style="{left: anLeft +'rem'}">
        <img src="@/assets/images/icon_sjb.png" alt />
      </div>
    </div>
    <div class="search-history" v-if="!isSearch">
      <div class="top">
        <span>历史记录</span>
        <i @click="clearHistory"></i>
      </div>
      <div class="bottom">
        <span v-for="(item, index) of historyList" :key="index" @click="search(item)">{{item}}</span>
      </div>
    </div>
    <Scroll
      class="search-scroll"
      ref="searchScroll"
      v-if="isSearch"
      :data="activityListAll"
      :pullUp="true"
      :listenScroll="true"
      :probeType="3"
      @scrollToEnd="handleScrollToEnd"
    >
      <div class="content">
        <div class="activity" v-if="activityListAll.length">
          <div class="act-tag">活动</div>
          <div v-for="(item, index) of activityListAll" :key="index" style="margin-bottom: 0.3rem">
            <panel-item-act :panelInfo="item" @click.native="panelItemClick(item)" />
          </div>
        </div>
        <div class="designer" v-if="designerListAll.length">
          <div class="dsg-tag">设计师</div>
          <div v-for="(item, index) of designerListAll" :key="index" style="margin-bottom: 0.3rem">
            <panel-item-designer
              :designerInfo="item"
              @click.native="$router.push({path: '/home/designer-detail', query: {id: item.id}})"
            />
          </div>
        </div>
        <div class="brand" v-if="brandListAll.length">
          <div class="brand-tag">品牌</div>
          <div v-for="(item, index) of brandListAll" :key="index" style="margin-bottom: 0.3rem">
            <panel-item-brand
              :brandInfo="item"
              @click.native="$router.push({path: '/home/brand-detail', query: {id: item.id}})"
            />
          </div>
        </div>
      </div>
      <div
        class="no-result"
        v-if="!activityListAll.length && !designerListAll.length && !brandListAll.length"
      >
        <img src="@/assets/images/1-zwnr.png" alt />
        <p>暂无搜索结果</p>
      </div>
    </Scroll>
  </div>
</template>
<script>
import PanelItemAct from '@/components/panel-item-act'
import PanelItemDesigner from '@/components/panel-item-desinger'
import PanelItemBrand from '@/components/panel-item-brand-row'
import Scroll from '@/components/scroll/scroll'
import {
  getSearchActivityList,
  getSearchDesignerList,
  getSearchBrandList
} from '@/api/my'
const ERR_OK = 200
export default {
  components: {
    PanelItemAct,
    PanelItemDesigner,
    PanelItemBrand,
    Scroll
  },
  data () {
    return {
      tabs: ['全部', '活动', '设计师', '品牌'],
      activeIndex: 0,
      anLeft: 0.56,
      isSearch: false,
      historyList: [],
      query: '',
      page: 1,
      activityListAll: [],
      designerListAll: [],
      brandListAll: [],
      total: 0,
      isMore: true,
      isHistorySearch: false
    }
  },
  directives: {
    focus: {
      // 指令的定义
      inserted: function (el) {
        el.focus()
      }
    }
  },
  watch: {
    query (newVal) {
      if (!newVal) {
        this.isSearch = false
        this.activityListAll = []
        this.designerListAll = []
        this.brandListAll = []
      }
    }
  },
  created () {
    if (localStorage.getItem('historyList')) {
      this.historyList = JSON.parse(localStorage.getItem('historyList'))
    }
  },
  beforeDestroy () {
    if (this.historyList.length) {
      localStorage.setItem('historyList', JSON.stringify(this.historyList))
    }
  },
  methods: {
    panelItemClick (item) {
      this.activityId = item.id
      this.$router.push({ name: 'ActivityDetail', params: { id: item.id } })
    },
    clearHistory () {
      this.$dialog
        .alert({
          message: '确认删除？'
        })
        .then(() => {
          // on close
          this.historyList = []
          localStorage.removeItem('historyList')
        })
    },
    handleScrollToEnd () {
      switch (this.activeIndex) {
        case 0:
          this.page = 1
          break
        case 1:
          this.page++
          if (this.isMore) {
            this._getSearchActivityList(this.query, this.page).then(res => {
              if (res.data.data.length) {
                this.$notify({
                  type: 'success',
                  message: `已为您更新${res.data.data.length}条数据`,
                  className: 'my-cls'
                })
              }
            })
          } else {
            this.$notify({
              type: 'warning',
              message: '没有更多内容了~',
              className: 'my-cls'
            })
          }
          break
        case 2:
          this.page++
          if (this.isMore) {
            this._getSearchDesignerList(this.query, this.page).then(res => {
              if (res.data.data.length) {
                this.$notify({
                  type: 'success',
                  message: `已为您更新${res.data.data.length}条数据`,
                  className: 'my-cls'
                })
              }
            })
          } else {
            this.$notify({
              type: 'warning',
              message: '没有更多内容了~',
              className: 'my-cls'
            })
          }
          break
        case 3:
          this.page++
          if (this.isMore) {
            this._getSearchBrandList(this.query, this.page).then(res => {
              if (res.data.data.length) {
                this.$notify({
                  type: 'success',
                  message: `已为您更新${res.data.data.length}条数据`,
                  className: 'my-cls'
                })
              }
            })
          } else {
            this.$notify({
              type: 'warning',
              message: '没有更多内容了~',
              className: 'my-cls'
            })
          }
          break
      }
    },
    hanldeBlur () {
      this.activeIndex = 0
      switch (this.activeIndex) {
        case 0:
          this.anLeft = 0.56
          break
      }
      this.isSearch = true
      this.activityListAll = []
      this.designerListAll = []
      this.brandListAll = []
      this.page = 1
      this.total = 0
      if (this.query && !this.historyList.includes(this.query)) {
        this.historyList.push(this.query)
      }
      if (!this.query) {
        this.isSearch = false
      }
      if (!this.isHistorySearch && this.query) {
        this._getSearchActivityList(this.query, this.page)
        this._getSearchDesignerList(this.query, this.page)
        this._getSearchBrandList(this.query, this.page)
      }
    },
    search (query) {
      this.isSearch = true
      this.isHistorySearch = true
      this.query = query
      this.activityListAll = []
      this.designerListAll = []
      this.brandListAll = []
      this.page = 1
      this.total = 0
      this._getSearchActivityList(this.query, this.page)
      this._getSearchDesignerList(this.query, this.page)
      this._getSearchBrandList(this.query, this.page)
    },
    tabItemClick (index) {
      this.activeIndex = index
      this.activityListAll = []
      this.designerListAll = []
      this.brandListAll = []
      this.isSearch = true
      this.page = 1
      this.total = 0
      if (this.$refs.searchScroll) {
        this.$refs.searchScroll.scrollTo(0, 0)
      }

      switch (index) {
        case 0:
          this._getSearchActivityList(this.query, this.page)
          this._getSearchDesignerList(this.query, this.page)
          this._getSearchBrandList(this.query, this.page)
          this.anLeft = 0.56
          break
        case 1:
          this._getSearchActivityList(this.query, this.page)
          this.anLeft = 1.84
          break
        case 2:
          this._getSearchDesignerList(this.query, this.page)
          this.anLeft = 3.25
          break
        case 3:
          this._getSearchBrandList(this.query, this.page)
          this.anLeft = 4.65
          break
      }
    },
    hasMore () {
      switch (this.activeIndex) {
        case 0:
        case 1:
          if (this.activityListAll.length === this.total) {
            this.isMore = false
          } else {
            this.isMore = true
          }
          break
        case 2:
          if (this.designerListAll.length === this.total) {
            this.isMore = false
          } else {
            this.isMore = true
          }
          break
        case 3:
          if (this.brandListAll.length === this.total) {
            this.isMore = false
          } else {
            this.isMore = true
          }
          break
      }
    },
    _getSearchActivityList (keywords, page) {
      return getSearchActivityList(keywords, page)
        .then(res => {
          if (res.code === ERR_OK) {
            // console.log(res)
            this.activityListAll = this.activityListAll.concat(res.data.data)
            this.total = res.data.total
            this.hasMore()
            return Promise.resolve(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    _getSearchDesignerList (keywords, page) {
      return getSearchDesignerList(keywords, page)
        .then(res => {
          if (res.code === ERR_OK) {
            this.designerListAll = this.designerListAll.concat(res.data.data)
            this.total = res.data.total
            this.hasMore()
            return Promise.resolve(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    _getSearchBrandList (keywords, page) {
      return getSearchBrandList(keywords, page)
        .then(res => {
          if (res.code === ERR_OK) {
            this.brandListAll = this.brandListAll.concat(res.data.data)
            this.total = res.data.total
            this.hasMore()
            return Promise.resolve(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/mixin.scss";
.search-wrapper {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: #f7f7f7;
  z-index: 10;
  touch-action: none;
  .search-box {
    width: 100%;
    padding: 0 0.36rem 0 0.36rem;
    box-sizing: border-box;
    background: #fff;

    .search-inner {
      display: flex;
      align-items: center;
      padding: 0.4rem 0 0.2rem 0.22rem;
      box-sizing: border-box;
      border-bottom: 1px solid #ccc;

      i {
        display: inline-block;
        width: 0.34rem;
        height: 0.34rem;
        background: url("../../assets/images/icon_ss.png") no-repeat;
        background-size: contain;
        vertical-align: bottom;
        margin-right: 0.21rem;
      }
      input {
        width: 100%;
        height: 100%;
        border: none;
        outline: none;
        font-size: 0.28rem;
        line-height: 0.34rem;
        font-family: PingFang SC;
        font-weight: 400;
        color: rgba(0, 4, 0, 1);
        line-height: 0.32rem;
        padding: 0.2rem 0;
        &::placeholder {
          color: rgba(153, 153, 153, 1);
        }
      }
    }
  }
  .tabs {
    width: 100%;
    height: 1rem;
    background: rgba(255, 255, 255, 1);
    padding: 0 0.36rem;
    box-sizing: border-box;
    display: flex;
    justify-content: flex-start;
    position: relative;
    .tab-item {
      font-size: 0.28rem;
      line-height: 0.28rem;
      font-family: PingFang SC;
      font-weight: 300;
      color: rgba(51, 51, 51, 1);
      text-align: center;
      margin-right: 0.71rem;
      padding: 0.36rem 0;
      &.active {
        font-weight: bold;
      }
    }
    .tab-angle {
      position: absolute;
      bottom: 0;
      width: 0.12rem;
      height: 0.06rem;
      transition: all 0.15s;
      img {
        width: 100%;
        vertical-align: top;
      }
    }
  }
  .search-scroll {
    position: absolute;
    left: 0;
    top: 2.5rem;
    right: 0;
    bottom: 0;
    overflow: hidden;
    .content {
      width: 100%;
      padding: 0.36rem;
      box-sizing: border-box;
      .activity,
      .designer,
      .brand {
        font-size: 0;
        .act-tag,
        .dsg-tag,
        .brand-tag {
          display: inline-block;
          border: 1px solid rgba(0, 0, 0, 1);
          font-size: 0.22rem;
          font-family: PingFang SC;
          font-weight: 300;
          color: rgba(51, 51, 51, 1);
          padding: 0.07rem 0.15rem;
          line-height: 0.22rem;
          margin-bottom: 0.27rem;
        }
      }
      .activity,
      .designer,
      .brand {
        margin-bottom: 0.72rem;
      }
    }
    .no-result {
      text-align: center;
      width: 3.84rem;
      height: 5.1rem;
      @include hCenter();
      top: 1.5rem;
      img {
        width: 100%;
      }
      p {
        font-size: 0.28rem;
        line-height: 0.5rem;
        text-align: center;
        font-family: PingFang SC;
        font-weight: 400;
        color: rgba(0, 4, 0, 1);
        @include hCenter();
        bottom: 0;
      }
    }
  }
  .search-history {
    width: 100%;
    position: absolute;
    left: 0;
    right: 0;
    top: 1.44rem;
    bottom: 0;
    padding: 0.5rem 0.36rem 0 0.36rem;
    box-sizing: border-box;
    background: #fff;
    .top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      span {
        font-size: 0.28rem;
        font-family: PingFang SC;
        font-weight: 400;
        color: rgba(0, 4, 0, 1);
        line-height: 0.32rem;
      }
      i {
        display: inline-block;
        width: 0.3rem;
        height: 0.34rem;
        background: url("../../assets/images/111.png") no-repeat;
        background-size: cover;
      }
    }
    .bottom {
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      margin-top: 0.31rem;
      span {
        padding: 0.2rem 0.39rem;
        font-size: 0.22rem;
        font-family: PingFang SC;
        font-weight: 400;
        color: rgba(0, 4, 0, 1);
        background: #f8f8f8;
        margin-right: 0.3rem;
        margin-bottom: 0.2rem;
      }
    }
  }
}
</style>
